package com.example.myfirstapplication.adapter

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.example.myfirstapplication.R
import com.njp.library.adapter.LoopAdapter

class BannerAdapter(private val context: Context, private val mData: ArrayList<Map<String, String>>) :
    LoopAdapter<BannerAdapter.MyViewHolder>() {
    class MyViewHolder(item: View) : RecyclerView.ViewHolder(item) {
        val bannerView: ImageView = item.findViewById(R.id.banner_img)
        val titleView: TextView = item.findViewById(R.id.banner_title)
    }

    override fun getRealItemCount(): Int {
        return mData.size
    }

    override fun onBindRealViewHolder(holder: MyViewHolder, realPosition: Int) {
        Glide.with(holder.bannerView).load(mData[realPosition]["url"]).into(holder.bannerView)
        holder.titleView.text = mData[realPosition]["title"]
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {
        val v = LayoutInflater.from(context).inflate(R.layout.banneritem_layout, parent, false)
        return MyViewHolder(v)
    }
}
